<?php
require_once 'common/config.php';
require_once 'common/functions.php';

// 检查登录状态
check_login();

// 检查是否是POST请求
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    die(json_encode(['success' => false, 'message' => '无效的请求方法']));
}

$id = isset($_POST['id']) ? intval($_POST['id']) : 0;

// 验证ID
if ($id <= 0) {
    die(json_encode(['success' => false, 'message' => '无效的教练ID']));
}

try {
    $db = db_connect();
    
    // 获取教练信息（主要是为了获取头像）
    $stmt = $db->prepare("SELECT avatar FROM coaches WHERE id = ?");
    $stmt->execute([$id]);
    $coach = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($coach) {
        // 删除头像文件
        if ($coach['avatar']) {
            $avatar_path = '../api/user/' . $coach['avatar'];
            if (file_exists($avatar_path)) {
                unlink($avatar_path);
            }
        }
        
        // 删除教练记录
        $stmt = $db->prepare("DELETE FROM coaches WHERE id = ?");
        $stmt->execute([$id]);
        
        die(json_encode(['success' => true]));
    } else {
        die(json_encode(['success' => false, 'message' => '教练不存在']));
    }
} catch(PDOException $e) {
    die(json_encode(['success' => false, 'message' => '系统错误，请稍后再试']));
} 